//设置ajax全局消息
$.ajaxSetup({
	global:true,
	error:function(jqXHR, textStatus, errorThrown)
	{
		console.log("请求错误："+textStatus+"   "+errorThrown);
		checkUserSession(jqXHR);
	},
	complete:function(XMLHttpRequest,textStatus){
		checkUserSession(XMLHttpRequest);
	}
});
function checkUserSession(XMLHttpRequest)
{
	// 通过XMLHttpRequest取得响应头，REDIRECT    
    var redirect = XMLHttpRequest.getResponseHeader("redirect");//若HEADER中含有REDIRECT说明后端想重定向  
    if (redirect == "true") {  
        var win = window;    
        while (win != win.top){  
            win = win.top;  
        }  
      //将后端重定向的地址取出来,使用win.location.href去实现重定向的要求  未登录时跳转到登录界面
      win.location.href= XMLHttpRequest.getResponseHeader("contentpath");    
    }  
}

/**
 * 管理后台主界面
 */
var adminVue;
var pageContentHeight=300;
var defaultPageSize=9; //默认分页为9
+(function(){
	 pageContentHeight=$("#pageContent").height();
	 initMenuTree();
	 adminVue=new Vue({
		el:"#mainContainerInner",
		data:{
			menuCollapse:false,
			menuNavs:[],   //[{name:"123"},{name:"234"}]
			loading:false,
			loadingTxt:$.admin.loadingTxt,
			resources:[]
		},
		methods:{
			onMenuItemClick:function(event) //菜单点击事件
			{
				if(event && event.currentTarget)
				{
					var menuUrl=$(event.currentTarget).attr("menu-url");
					var obj=this.getMenuNavs(event.currentTarget);
					if(menuUrl!=null && menuUrl!="#")
					{
						this.loadMenu(menuUrl);
					}
				}
			},
			getMenuNavs:function(ele)
			{
				var navs=[];
				var navName=$(ele).find("span").text();
				var menuUrl=$(ele).attr("menu-url");
				navs.push({name:navName,menuUrl:menuUrl,type:"menu"});
				$(ele).parents(".el-submenu").each(function(index,e){
					var tname=$(this).find(".el-submenu__title span").text();
					navs.push({name:tname,type:"catagory"});
				});
				this.menuNavs=navs.reverse();
			},
		   loadMenu:function(menuUrl) //加载菜单
		   {
			   $("#pageContent").empty();
			   if(menuUrl)
				{
				    var $this=this;
				    $this.loading=true;
					$("#pageContent").load(menuUrl,function(responseText, textStatus, XMLHttpRequest)
					{
						$this.loading=false;
					});
				}
		   },
		   mainPage:function(event)
		   {
			   var url=ctx+"/admin/user/online/index";
			   this.loadMenu(url);
		   },
		   onBreadcrumbsNavClick:function(event) //点击中上部的导航
		   {
			   var $parent=$(event.currentTarget).parent();
			   var menuUrl=$parent.attr("menuUrl");
			   var menuType=$parent.attr("type");
			   if($parent.next().length>0 && menuUrl)
			   {
				   this.loadMenu(menuUrl);
				   if(menuType=="index") this.menuNavs=[];
			   }
		   }
		   
		}
	});
	
	$("#menuBar .el-menu-item").click(adminVue.onMenuItemClick);
	$("#breadcrumbs .el-breadcrumb__item__inner").click(adminVue.onBreadcrumbsNavClick);
	
	adminVue.mainPage();
	
	//初始菜单树
	function initMenuTree(){ 
		   var $this=this;
		   var url=ctx+"/admin/resource/userOwnResource";
		   $.ajax({
			   url:url,type:"POST",dataType:"json",async:false,data:{},success:function(data)
			   {
				   $this.resources=data.data;
				   if($this.resources && $this.resources.length>0)
					{
					   var resourceTree=$.util.listToTree($this.resources,"id","parent_id");
					   var menuHtml=generateMenuHtml(resourceTree);
					   $(".menu-vertical").html(menuHtml);
					}
			   }
		   });
	   }
	//生成菜单html
	function generateMenuHtml(list) 
    {
	   var menuHtml="";
	   for(var i=0;i<list.length;i++)
	   {
		   var item=list[i];
		   var menu="";
		   var children=item.children;
		   if(children && children.length>0)
		   {
			   menu=' <el-submenu index="'+item.id+'"> '+
			   ' <template slot="title"> '+
			   ' <i class="menu-icon '+(item.icon ? item.icon:"fa fa-leaf")+'"></i> '+
			   ' <span  class="menu-name" slot="title">'+item.name+'</span> '+
			   ' </template> ';
			   menu+=generateMenuHtml(item.children); 
			   menu+=' </el-submenu> ';
		   }
		   else
		   {
			   menu+=' <el-menu-item index="'+item.id+'"  menu-url="'+(item.url ? ctx+item.url : "#")+'"><i class="menu-icon '+(item.icon ? item.icon:"fa fa-leaf")+'"></i><span class="menu-name">'+item.name+'</span></el-menu-item> '; 
		   }
		   menuHtml+=menu;
	   }
	   return menuHtml;
    }
})();